package com.charger.giftcert.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.PromoteOperations.domain.GiftRuleofgift;
import com.charger.PromoteOperations.domain.GiftcertificateChargingstation;
import com.charger.PromoteOperations.domain.Roleofgift;
import com.charger.PromoteOperations.service.GiftRuleofgiftService;
import com.charger.PromoteOperations.service.GiftcertificateChargingstationService;
import com.charger.PromoteOperations.service.RoleofgiftService;
import com.charger.chargingStation.domain.Chargingstation;
import com.charger.chargingStation.service.IChargingstationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.giftcert.domain.Giftcertificate;
import com.charger.giftcert.service.IGiftcertificateService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 礼券管理Controller
 * 
 * @author Lilysi
 * @date 2024-11-23
 */
@RestController
@RequestMapping("/giftcert/giftcertificate")
public class GiftcertificateController extends BaseController
{
//    礼券管理表
    @Autowired
    private IGiftcertificateService giftcertificateService;
//    充电站
    @Autowired
    private IChargingstationService chargingstationService;
//    礼券规则中间表
    @Autowired
    private GiftRuleofgiftService giftRuleofgiftService;
//    礼券规则表
    @Autowired
    private RoleofgiftService roleofgiftService;
//    礼券充电站中间表
    @Autowired
    GiftcertificateChargingstationService giftcertificateChargingstationService;
    /**
     * 查询礼券管理列表
     */
    @PreAuthorize("@ss.hasPermi('giftcert:giftcertificate:list')")
    @GetMapping("/list")
    public TableDataInfo list(Giftcertificate giftcertificate)
    {
        startPage();
        List<Giftcertificate> list = giftcertificateService.selectGiftcertificateList(giftcertificate);
        for (Giftcertificate gift:list){
            Long giftId = gift.getId();
            QueryWrapper<GiftRuleofgift> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("gift_id",giftId);
            List<GiftRuleofgift> giftRuleofgifts = giftRuleofgiftService.list(queryWrapper);
            List<Roleofgift> ruleList=new ArrayList<>();
            for (GiftRuleofgift giftRuleofgift:giftRuleofgifts){
                Long ruleId = giftRuleofgift.getRuleId();
                Roleofgift byId = roleofgiftService.getById(ruleId);
                ruleList.add(byId);
            }
            gift.setRoleofgifts(ruleList);
            QueryWrapper<GiftcertificateChargingstation> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("giftcertificate_id",giftId);
            List<Chargingstation> chargstationList = new ArrayList();
            List<GiftcertificateChargingstation> giftcertificateChargingstations = giftcertificateChargingstationService.list(queryWrapper2);
            for (GiftcertificateChargingstation giftcertificateChargingstation:giftcertificateChargingstations){
                Long chargingstationId = giftcertificateChargingstation.getChargingstationId();
                Chargingstation chargingstation = chargingstationService.selectChargingstationByChargingstationId(chargingstationId);
                chargstationList.add(chargingstation);
            }
            gift.setChargingstations(chargstationList);
        }
        return getDataTable(list);
    }

    /**
     * 导出礼券管理列表
     */
    @PreAuthorize("@ss.hasPermi('giftcert:giftcertificate:export')")
    @Log(title = "礼券管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Giftcertificate giftcertificate)
    {
        List<Giftcertificate> list = giftcertificateService.selectGiftcertificateList(giftcertificate);
        ExcelUtil<Giftcertificate> util = new ExcelUtil<Giftcertificate>(Giftcertificate.class);
        util.exportExcel(response, list, "礼券管理数据");
    }

    /**
     * 获取礼券管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('giftcert:giftcertificate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Giftcertificate gift = giftcertificateService.selectGiftcertificateById(id);
        Long giftId = gift.getId();
        QueryWrapper<GiftRuleofgift> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gift_id",giftId);
        List<GiftRuleofgift> giftRuleofgifts = giftRuleofgiftService.list(queryWrapper);
        List<Roleofgift> ruleList=new ArrayList<>();
        for (GiftRuleofgift giftRuleofgift:giftRuleofgifts){
            Long ruleId = giftRuleofgift.getRuleId();
            Roleofgift byId = roleofgiftService.getById(ruleId);
            ruleList.add(byId);
        }
        gift.setRoleofgifts(ruleList);
        QueryWrapper<GiftcertificateChargingstation> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("giftcertificate_id",giftId);
        List<Chargingstation> chargstationList = new ArrayList();
        List<GiftcertificateChargingstation> giftcertificateChargingstations = giftcertificateChargingstationService.list(queryWrapper2);
        for (GiftcertificateChargingstation giftcertificateChargingstation:giftcertificateChargingstations){
            Long chargingstationId = giftcertificateChargingstation.getChargingstationId();
            Chargingstation chargingstation = chargingstationService.selectChargingstationByChargingstationId(chargingstationId);
            chargstationList.add(chargingstation);
        }
        gift.setChargingstations(chargstationList);
        return AjaxResult.success(gift);
    }

    /**
     * 新增礼券管理
     * 在增加时向礼券-规则中间表和礼券-充电站中间表插入数据
     */
    @PreAuthorize("@ss.hasPermi('giftcert:giftcertificate:add')")
    @Log(title = "礼券管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Giftcertificate giftcertificate)
    {
        int i = giftcertificateService.insertGiftcertificate(giftcertificate);
        Long gift_id = giftcertificate.getId();
        List<Long> roleofgiftId = giftcertificate.getRoleofgiftId();
        if (roleofgiftId != null){
            for (Long roleofgift_id : roleofgiftId){
                GiftRuleofgift giftRuleofgift = new GiftRuleofgift();
                giftRuleofgift.setGiftId(gift_id);
                giftRuleofgift.setRuleId(roleofgift_id);
                giftRuleofgiftService.save(giftRuleofgift);
            }
        }
        List<Long> chargingstationId = giftcertificate.getChargerStationId();
        if (chargingstationId != null){
            for (Long chargingstation_id : chargingstationId){
                GiftcertificateChargingstation giftcertificateChargingstation = new GiftcertificateChargingstation();
                 giftcertificateChargingstation.setChargingstationId(chargingstation_id);
                 giftcertificateChargingstation.setGiftcertificateId(gift_id);
                 giftcertificateChargingstationService.save(giftcertificateChargingstation);
            }
        }
        return toAjax(i);
    }

    /**
     * 修改礼券管理
     */
    @PreAuthorize("@ss.hasPermi('giftcert:giftcertificate:edit')")
    @Log(title = "礼券管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Giftcertificate giftcertificate)
    {
//        删除对应礼券-规则中间表和礼券-充电站中间表数据
        int i = giftcertificateService.updateGiftcertificate(giftcertificate);
        Long gift_id = giftcertificate.getId();
        QueryWrapper<GiftRuleofgift> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gift_id",gift_id);
        giftRuleofgiftService.remove(queryWrapper);
        QueryWrapper<GiftcertificateChargingstation> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("giftcertificate_id",gift_id);
        giftcertificateChargingstationService.remove(queryWrapper2);
//        增加新的礼券-规则中间表和礼券-充电站中间表数据
        List<Long> roleofgiftId = giftcertificate.getRoleofgiftId();
        if (roleofgiftId != null){
            for (Long roleofgift_id : roleofgiftId){
                GiftRuleofgift giftRuleofgift = new GiftRuleofgift();
                giftRuleofgift.setGiftId(gift_id);
                giftRuleofgift.setRuleId(roleofgift_id);
                giftRuleofgiftService.save(giftRuleofgift);
            }
        }
        List<Long> chargingstationId = giftcertificate.getChargerStationId();
        if (chargingstationId != null){
            for (Long chargingstation_id : chargingstationId){
                GiftcertificateChargingstation giftcertificateChargingstation = new GiftcertificateChargingstation();
                giftcertificateChargingstation.setChargingstationId(chargingstation_id);
                giftcertificateChargingstation.setGiftcertificateId(gift_id);
                giftcertificateChargingstationService.save(giftcertificateChargingstation);
            }
        }
        return toAjax(i);
    }

    /**
     * 删除礼券管理
     */
    @PreAuthorize("@ss.hasPermi('giftcert:giftcertificate:remove')")
    @Log(title = "礼券管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(giftcertificateService.deleteGiftcertificateByIds(ids));
    }
}
